import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:heytea_kit/heytea_kit.dart';
import 'package:heytea_kit/heytea_localizations.dart';
import 'package:heytea_kit/widget/datetime.dart';
import 'package:heytea_kit/widget/location/poi_list.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AmapService.instance.init(
    iosKey: '326bb4cfbaa3a3087e56a5e178383d3a',
    androidKey: '9b1fdab026e4998b744b366947e6f13f',
  );

  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _poiController = ValueNotifier<Poi>(null);
  final _centerController = ValueNotifier<LatLng>(LatLng(22.55, 114.05));
  final _keywordController = ValueNotifier<String>('');

  @override
  void initState() {
    super.initState();

    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await HeyTeaKit.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        HeyTeaLocalizations.delegate,
      ],
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Stack(
          children: [
            PoiList(
              poiController: _poiController,
              centerController: _centerController,
            ),
            DateTimeText(DateTime(2021, 1, 1)),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            _centerController.value = LatLng(22.55, 114.05);
          },
        ),
      ),
    );
  }
}
